﻿using System;
using System.Collections.Generic;
using System.Linq;
using ud_Utilities;

namespace ud_SwirlingQuestion.Business
{
    public class DBSwirlingQuestion
    {
        readonly SwirlingQuestionDC db = new SwirlingQuestionDC();

        public ud_QA GetOne(Guid itemGuid)
        {
            var result = from p in db.ud_QAs
                         where p.Guid.Equals(itemGuid)
                         select p;
            return result.FirstOrDefault();
        }

        public List<ud_QA> GetAll(int moduleID)
        {
            var result = from p in db.ud_QAs
                         where p.ModuleID.Equals(moduleID)
                         orderby p.LastModified descending
                         select p;
            return result.ToList();
        }

        public List<ud_QA> GetAllUnpublised(int moduleID)
        {
            var result = from p in db.ud_QAs
                         where p.ModuleID.Equals(moduleID) && !p.IsPublished
                         orderby p.LastModified descending
                         select p;
            return result.ToList();
        }

        public List<ud_QA> GetAllPublished(int moduleID)
        {
            var result = from p in db.ud_QAs
                         where p.ModuleID.Equals(moduleID) && p.IsPublished
                         orderby p.LastModified descending
                         select p;
            return result.ToList();
        }

        public List<ud_QA> GetAllOthers(int moduleID, Guid itemGuid)
        {
            var result = from p in db.ud_QAs
                         where p.ModuleID.Equals(moduleID) && p.Guid != itemGuid && p.IsPublished
                         orderby p.LastModified descending
                         select p;
            return result.ToList();
        }

        public List<ud_QA> GetPage(int moduleID, int pageSize, int currentPage, out int totalPages)
        {
            var result = from p in db.ud_QAs
                         where p.ModuleID.Equals(moduleID) && p.IsPublished
                         orderby p.LastModified descending
                         select p;
            totalPages = result.Count() / pageSize;
            if ((result.Count() % pageSize) != 0 && result.Count() > pageSize)
            {
                totalPages++;
            }
            return result.Skip((currentPage - 1) * pageSize).Take(pageSize).ToList();
        }

        public List<ud_QA> GetOthersPage(int moduleID, int pageSize, int currentPage)
        {
            var result = from p in db.ud_QAs
                         where p.ModuleID.Equals(moduleID) && p.IsPublished
                         orderby p.LastModified descending
                         select p;
            return result.Take((currentPage - 1) * pageSize).Skip(pageSize).ToList();
        }

        public bool Delete(Guid itemGuid)
        {
            bool flag = false;
            ud_QA item = GetOne(itemGuid);
            db.ud_QAs.DeleteOnSubmit(item);
            try
            {
                db.SubmitChanges();
                flag = true;
            }
            catch { }
            return flag;
        }

        public bool Create(int moduleID, string title, string question, string answer, bool isPublished, string createdName, string createdEmail, string createdPhone, int answerUserID, string itemUrl, bool isHot, out Guid itemGuid)
        {
            return Create(moduleID, title, question, answer, isPublished, createdName, createdEmail, createdPhone,
                          answerUserID, itemUrl, isHot, out itemGuid, DateTime.UtcNow);
        }

        public bool Create(int moduleID, string title, string question, string answer, bool isPublished, string createdName, string createdEmail, string createdPhone, int answerUserID, string itemUrl, bool isHot, out Guid itemGuid, DateTime modifiedDate)
        {
            bool flag = false;

            ud_QA item = new ud_QA
            {
                Title = title,
                Guid = Guid.NewGuid(),
                ModuleID = moduleID,
                Question = question,
                Answer = answer,
                IsPublished = isPublished,
                CreatedByName = createdName,
                CreatedByEmail = createdEmail,
                CreatedByPhone = createdPhone,
                AnswerUser = answerUserID,
                ItemUrl = itemUrl,
                IsHot = isHot,
                LastModified = modifiedDate
            };
            itemGuid = item.Guid;
            db.ud_QAs.InsertOnSubmit(item);
            try
            {
                db.SubmitChanges();
                flag = true;
            }
            catch { }
            return flag;
        }

        public bool Update(Guid itemGuid, string title, string question, string answer, bool isPublished, string createdName, string createdEmail, string createdPhone, int answerUserID, string itemUrl, bool isHot)
        {
            return Update(itemGuid, title, question, answer, isPublished, createdName, createdEmail, createdPhone,
                          answerUserID, itemUrl, isHot, DateTime.UtcNow);
        }

        public bool Update(Guid itemGuid, string title, string question, string answer, bool isPublished, string createdName, string createdEmail, string createdPhone, int answerUserID, string itemUrl, bool isHot, DateTime modifiedDate)
        {
            bool flag = false;

            ud_QA item = GetOne(itemGuid);
            if (item != null)
            {
                item.Title = title;
                item.Question = question;
                item.Answer = answer;
                item.IsPublished = isPublished;
                item.CreatedByName = createdName;
                item.CreatedByEmail = createdEmail;
                item.CreatedByPhone = createdPhone;
                item.AnswerUser = answerUserID;
                item.ItemUrl = itemUrl;
                item.IsHot = isHot;
                item.HitCount++;
                item.LastModified = modifiedDate;
                try
                {
                    db.SubmitChanges();
                    flag = true;
                }
                catch { }
            }
            return flag;
        }

        public bool IncreaseCommentCount(Guid itemGuid)
        {
            bool flag = false;

            ud_QA item = GetOne(itemGuid);
            if (item != null)
            {
                item.CommentCount++;
                try
                {
                    db.SubmitChanges();
                    flag = true;
                }
                catch { }
            }
            return flag;
        }

        public List<ud_QA> Search(int siteID, int moduleID, string searchTerm)
        {
            List<ud_QA> result = new List<ud_QA>();
            searchTerm = searchTerm.ToLowerInvariant();
            
                var listModuleID = from p in db.mp_Modules
                                   where p.SiteID == siteID
                                   select p.ModuleID;
                List<ud_QA> allQA = (from p in db.ud_QAs
                              where listModuleID.ToList().Contains(p.ModuleID)
                              select p).ToList();
                foreach (ud_QA qa in allQA)
                {
                    if (qa.Title.ToLowerInvariant().Contains(searchTerm) ||
                        qa.Question.ToLowerInvariant().Contains(searchTerm) ||
                        qa.Answer.ToLowerInvariant().Contains(searchTerm) ||
                        Utilities.ConvertToUnsign(qa.Title).ToLowerInvariant().Contains(searchTerm) ||
                        Utilities.ConvertToUnsign(qa.Question).ToLowerInvariant().Contains(searchTerm) ||
                        Utilities.ConvertToUnsign(qa.Answer).ToLowerInvariant().Contains(searchTerm) ||
                        searchTerm == string.Empty)
                    {
                        result.Add(qa);
                    }
                }
            return result;
        }

        public void DeleteByModule(int moduleID)
        {
            var result = from p in db.ud_QAs
                         where p.ModuleID.Equals(moduleID)
                         select p;
            db.ud_QAs.DeleteAllOnSubmit(result);
            db.SubmitChanges();
        }
    }
}
